package com.zimmor.mq.rocket_train.order;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import sun.rmi.log.LogInputStream;

import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2023/2/10 17:55
 * @Description 发送顺序消息
 * @version: 1.0.0
 */

public class Producer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("producer-group4");
        producer.setNamesrvAddr("192.168.204.136:9876;192.168.204.137:9876");
        producer.start();
        String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
        for (int i = 100; i > 0; i--) {
            int orderid = i % 10;
            Message message = new Message("order", tags[orderid % tags.length], orderid + "", ("order-message" + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult send = producer.send(message, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object arg) {
                    Integer id = (Integer) arg;
                    System.out.println("id:" + id);
                    return list.get(id % list.size());
                }
            }, orderid);
            System.out.println("send = " + send + "tag:" + message.getTags());
        }
        producer.shutdown();

    }
}
